<template>
  <div class="fund-setting">
    <avue-form
      ref="fundFormRef"
      :option="option"
      v-model="form"
      @submit="handleSubmit"
    ></avue-form>
    <div class="label">
      <span>充值金额快捷设置</span>
    </div>
    <avue-crud
      ref="fundListRef"
      :table-loading="tableLoading"
      v-model="form_deposit"
      :option="option_deposit"
      @refresh-change="getList"
      @row-save="rowSave"
      @row-update="rowUpdate"
      @row-del="rowDel"
      :data="data"
    ></avue-crud>
  </div>
</template>

<script setup>
import { ref, getCurrentInstance, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import {
  getGlobalFundSet,
  editGlobalFundSet,
  getDepositConfig,
  addDepositConfig,
  editDepositConfig,
  delDepositConfig,
} from "@/apis/admin";
let { proxy } = getCurrentInstance();
const option = ref({
  labelWidth: 120,
  column: [
    {
      label: "首充送积分",
      prop: "firstRechargePoint",
      span: 6,
      type: "number",
      rules: [
        {
          required: true,
          message: "请输入首充赠送积分",
          trigger: "blur",
        },
      ],
    },
    {
      label: "注册送积分",
      prop: "registerPoint",
      span: 6,
      type: "number",
      rules: [
        {
          required: true,
          message: "请输入注册赠送积分",
          trigger: "blur",
        },
      ],
    },
    {
      label:"轮盘消耗积分",
      prop: "spinCost",
      span: 6,
      type: "number",
      rules: [
        {
          required: true,
          message: "请输入轮盘消耗积分",
          trigger: "blur",
        },
      ],
    },
    {
      label: "最低充值金额",
      prop: "minRecharge",
      span: 6,
      type: "number",
      rules: [
        {
          required: true,
          message: "请输入最低充值金额",
          trigger: "blur",
        },
      ],
    },

    {
      label: "最低提现金额",
      prop: "minWithdraw",
      span: 6,
      type: "number",
      rules: [
        {
          required: true,
          message: "请输入最低提现金额",
          trigger: "blur",
        },
      ],
    },
    {
      label: "自动审核金额",
      prop: "autoWithdraw",
      span: 6,
      type: "number",
      rules: [
        {
          required: true,
          message: "请输入自动审核金额",
          trigger: "blur",
        },
      ],
    },
    {
      label: "自动审核/日次",
      prop: "autoDayWithdraw",
      span: 6,
      type: "number",
      rules: [
        {
          required: true,
          message: "请输入每日自动审核次数",
          trigger: "blur",
        },
      ],
    },
    {
      label: "游戏赢的倍数",
      prop: "gameWinRate",
      span: 6,
      type: "number",
      rules: [
        {
          required: true,
          message: "请输入游戏赢的倍数",
          trigger: "blur",
        },
      ],
    },
    {
      label: "提现自动审核",
      prop: "isAutoWithdraw",
      span: 6,
      type: "switch",
    },
    {
      label: "支付维护",
      prop: "payMaintain",
      span: 7,
      type: "switch"
    },
  ],
});
const form = ref({
  firstRechargePoint: 0,
  registerPoint: 0,
  minRecharge: 0,
  minWithdraw: 0,
  isAutoWithdraw: false,
  payMaintain:false,
  autoWithdraw: 0,
  spinCost: 0,
  autoDayWithdraw:0,
  gameWinRate:0
});
const handleSubmit = (form, done) => {
  editGlobalFundSet
    .post("", {
      ...form,
      firstRechargePoint: form.firstRechargePoint,
      registerPoint: form.registerPoint,
      payMaintain:form.payMaintain ? 1 : 0,
      minRecharge: Number((form.minRecharge * 100).toFixed(0)),
      minWithdraw: Number((form.minWithdraw * 100).toFixed(0)),
      autoWithdraw: Number((form.autoWithdraw * 100).toFixed(0)),
      isAutoWithdraw: form.isAutoWithdraw ? 1 : 0,
      spinCost: form.spinCost,
      autoDayWithdraw:Number(form.autoDayWithdraw),
      gameWinRate:Number(form.gameWinRate)
    })
    .then((res) => {
      done();
      if (res.code == 200) {
        ElMessage.success("修改已保存");
        getConfig();
      }
    });
};

const tableLoading = ref(false);
const data = ref([]);
const form_deposit = ref({});
const option_deposit = ref({
  index: true,
  border: true,
  stripe: true,
  menuAlign: "center",
  loadingText: "加载中...",
  loadingSpinner: "svg",
  loadingSvgViewBox: "-10, -10, 50, 50",
  loadingBackground: "rgba(122, 122, 122, 0.6)",
  searchShow: false,
  columnBtn: false,
  refreshBtn: false,
  column: [
    {
      label: "实付金额",
      prop: "amount",
      type: "number",
      rules: [
        {
          required: true,
          message: "请输入实付金额",
          trigger: "blur",
        },
      ],
    },
    {
      label: "赠送金额",
      prop: "gift",
      type: "number",
      rules: [
        {
          required: true,
          message: "请输入赠送金额",
          trigger: "blur",
        },
      ],
    },
    {
      label: "是否推荐",
      prop: "isRecommend",
      type: "select",
      rules: [
        {
          required: true,
          message: "请选择是否推荐",
          trigger: "change",
        },
      ],
      dicData: [
        {
          label: "否",
          value: 1,
        },
        {
          label: "是",
          value: 2,
        },
      ],
      formatter: (row, value, column) => {
        return row.isRecommend === 2 ? "是" : "否";
      },
    },
    {
      label: "首次赠送",
      prop: "isFirstGift",
      type: "select",
      rules: [
        {
          required: true,
          message: "是否首次赠送",
          trigger: "change",
        },
      ],
      dicData: [
        {
          label: "否",
          value: 1,
        },
        {
          label: "是",
          value: 2,
        },
      ],
      formatter: (row, value, column) => {
        return row.isFirstGift === 2 ? "是" : "否";
      },
    },
  ],
});
const rowSave = async (row, done, loading) => {
  const res = await addDepositConfig.post("", {
    ...row,
    amount: Number((row.amount * 100).toFixed(0)),
    gift: Number((row.gift * 100).toFixed(0)),
  });
  if (res.code == 200) {
    ElMessage.success("添加成功");
    getList();
  }
  done();
};
const rowDel = (row, index, done) => {
  ElMessageBox.confirm("此操作将永久删除该配置, 是否继续?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    delDepositConfig.post("", { id: row.id }).then((res) => {
      if (res.code == 200) {
        ElMessage({
          message: "已删除",
          type: "success",
        });
        getList();
      }
    });
  });
};
const rowUpdate = async (row, index, done, loading) => {
  console.log('row',row)
  const res = await editDepositConfig.post("", {
    ...row,
   
    amount: Number((row.amount * 100).toFixed(0)),
    gift: Number((row.gift * 100).toFixed(0)),
  });
  if (res.code == 200) {
    ElMessage.success("编辑成功");
    getList();
  }
  done();
};
const getList = () => {
  tableLoading.value = true;
  getDepositConfig.get("", {}).then((res) => {
    tableLoading.value = false;
    if (res.code == 200) {
      res.data.list.map((item) => {
        item.amount = item.amount / 100;
        item.gift = item.gift / 100;
      });
      data.value = res.data.list;
    }
  });
};
const getConfig = () => {
  getGlobalFundSet.get("", {}).then((res) => {
    if (res.code == 200) {
      res.data.isAutoWithdraw == 1 ? (res.data.isAutoWithdraw = true) : false;
      res.data.payMaintain == 1 ? (res.data.payMaintain = true) : false;
      res.data.minRecharge = res.data.minRecharge / 100;
      res.data.minWithdraw = res.data.minWithdraw / 100;
      res.data.autoWithdraw = res.data.autoWithdraw / 100;
      form.value = res.data;
    }
  });
};
onMounted(() => {
  getConfig();
  getList();
});
</script>

<style lang="scss" scoped>
.fund-setting {
  width: 100%;
  .label {
    margin: 15px 0;
    width: 100%;
    border-radius: 8px;
    box-sizing: border-box;
    padding: 15px 20px;
    background-color: #f5f5f5;
    color: #000;
    font-size: 16px;
    font-weight: 600;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
</style>
